package com.inkegz.network

import com.meelive.ingkee.logger.IKLog
import okhttp3.HttpUrl

private val hostField = HttpUrl::class.java.getDeclaredField("host").apply {
    isAccessible = true
}

private val schemeField = HttpUrl::class.java.getDeclaredField("scheme").apply {
    isAccessible = true
}

private val portField = HttpUrl::class.java.getDeclaredField("port").apply {
    isAccessible = true
}

/**
 * 反射修改 httpUrl 的host
 *
 * 反射失败请检查是否混淆
 */
fun changeHttpUrlHost(httpUrl: HttpUrl, host: String){
    try {
        hostField.set(httpUrl, host)
    }catch (_: Exception){
        IKLog.e("please check okhttp3.HttpUrl proguard")
    }
}

/**
 * 反射修改 httpUrl 的 scheme
 *
 * 反射失败请检查是否混淆
 */
fun changeHttpUrlScheme(httpUrl: HttpUrl, scheme: String){
    try {
        schemeField.set(httpUrl, scheme)
    }catch (_: Exception){
        IKLog.e("please check okhttp3.HttpUrl proguard")
    }
}

fun changeHttpUrlPort(httpUrl: HttpUrl, port: Int){
    try {
        portField.set(httpUrl, port)
    }catch (_: Exception){
        IKLog.e("please check okhttp3.HttpUrl proguard")
    }
}